package com.dragon.mmochat.message;

import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatPlayerShopType;
import com.dragon.mmochat.model.shop.MmochatPlayerShop;
import com.dragon.mmochat.service.MmochatFilterService;
import com.dragon.mmochat.service.MmochatShopService;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_19_1S extends SkymobiProtocolMessage {
	int hallState;
	MmochatPlayerShopType type;
	List<MmochatPlayerShop> shopList = null;
	int curPage;

	public Mmochat_19_1S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_19_1);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (shopList == null) {
			return;
		}
		out.putUnsignedShort(hallState);
		int shopNum = shopList.size();
		int pageNum = (int) Math.ceil(1.0 * shopNum
				/ MmochatConstant.shopNumShowPerPage);
		pageNum = Math.max(1, pageNum);
		curPage = Math.min(curPage, pageNum - 1);
		curPage = Math.max(curPage, 0);
		int showShopNum = Math.min(shopNum - curPage
				* MmochatConstant.shopNumShowPerPage,
				MmochatConstant.shopNumShowPerPage);
		out.putPrefixedString(type.toString(), 1);
		out.putInt(type.getKey());
		out.putInt(pageNum);
		out.putInt(curPage);
		out.put((byte) showShopNum);
		if (showShopNum > 0) {
			int baseIndex = curPage * MmochatConstant.shopNumShowPerPage;
			for (int i = 0; i < showShopNum; i++) {
				MmochatPlayerShop shop = shopList.get(i + baseIndex);
				out.putInt(shop.getId());
				out.putPrefixedString(shop.getName(), 1);
				out.putPrefixedString(MmochatShopService.getShopStar(shop), 1);
				out.put((byte) shop.getPageNum());
				if (shop.getIntro() == null) {
					out.putPrefixedString("", 2);
				} else {
					String intro = MmochatFilterService.getFilterString(shop.getIntro());
					out.putPrefixedString(intro, 2);
				}
			}
		}
	}

	public void setHallState(int hallState) {
		this.hallState = hallState;
	}

	public void setShopList(List<MmochatPlayerShop> shopList) {
		this.shopList = shopList;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public void setType(MmochatPlayerShopType type) {
		this.type = type;
	}

}
